package com.zhangwei.day43bluetooth.ble;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.zhangwei.day43bluetooth.R;

import java.util.ArrayList;

/**
 * 低功耗
 */
public class BLEActivity extends AppCompatActivity {
    BluetoothAdapter bluetoothAdapter;
    ArrayList<BluetoothDevice> devices = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ble);
        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(BLEActivity.this, "当前设备不支持", Toast.LENGTH_SHORT).show();
            this.finish();
        }
        BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

        if (manager != null) {
            bluetoothAdapter = manager.getAdapter();
        }
        //打开蓝牙设备
        if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
            bluetoothAdapter.enable();
        }
        //扫描附近设备


    }

    private void scanDevice(boolean enable) {
        //开始扫描设备
        bluetoothAdapter.startLeScan(callback);
        //停止扫描设备
        bluetoothAdapter.stopLeScan(callback);
    }

    private MyLeScanCallback callback = new MyLeScanCallback();

    private class MyLeScanCallback implements BluetoothAdapter.LeScanCallback {

        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            devices.add(device);
        }
    }


}
